using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using MacomberMap.Common.Components;
using MacomberMap.Common.Internals;

namespace MacomberMap.Common.Convertors
{
    /// <summary>
    /// This class allows for substation location
    /// </summary>
    public class MM_Substation_Locator : TypeConverter
    {

        /// <summary>
        /// Notify the system we can retrieve standard values
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// Notify the system we can retrieve standard values exclusively
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// Return a standard values collection of substations
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(MM_Repository.Substations.Values);
        }

        /// <summary>
        /// Indicate we can convert a substation into a string
        /// </summary>
        /// <param name="context"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(MM_Substation))
                return true;
            else
                return base.CanConvertTo(context, destinationType);
        }


        /// <summary>
        /// Convert a substation to a string
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is MM_Substation)
            {
                MM_Substation InSub = (MM_Substation)value;
                return InSub.ToString();
            }
            else
                return base.ConvertTo(context, culture, value, destinationType);
        }

        /// <summary>
        /// Indicate we can convert a string to a substation
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sourceType"></param>
        /// <returns></returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;
            else
                return base.CanConvertFrom(context, sourceType);
        }


        /// <summary>
        /// Convert from a string to a substation
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is String)
            {
                String StrToCheck = (value as string).Substring(0, (value as string).IndexOf(" (")).Trim();
                if (MM_Repository.Substations.ContainsKey(StrToCheck))
                    return MM_Repository.Substations[StrToCheck];
                else
                    return null;
            }
            else
                return base.ConvertFrom(context, culture, value);
        }
    }
}
